package Sys_zakupok_br_obl;
use strict;
use warnings;
use Encode;
use Data::Dumper;
use WWW::Mechanize;

my $enabled = 0; # 1/0 - edit to enable/disable module
my $site_address = 'http://84.42.41.86';


sub explore{
 my ( $self, $c ) = @_;
 return 1 if !$enabled;
 
 $c->debug_call("call explore conf=".$c->conf('user_agent')."\n");
 

 my $news = {
	site => $site_address,
	id => undef,
	url => undef,
	small_text => undef,
	customer => undef,
 }; # - base s

 
 $c->debug_explain(" new WWW_Mechanize\n");
 my $m = WWW::Mechanize->new();
 #my $url = "$site_address/Tender/Purchase.aspx";
 #my $url = "$site_address/Tender/Purchase.aspx?LevelId=1";

 my $total_inserted = 0; # сколько всего залито новостей
 my @urls = ("$site_address/Tender/Purchase.aspx","$site_address/Tender/Purchase.aspx?LevelId=1"); # разделы идентичной структуры по которым пройтись
 for my $url ( @urls ){
    my @pages = (1..10); # у каждого раздела несколько страниц их желательно пролистать
    for my $page_n ( @pages ){
	my $current_inserted = $self->insert_from_page( c=>$c, news=>$news, m=>$m, url=>$url, page_n=>$page_n );
	defined $current_inserted or return 0;
	$total_inserted += $current_inserted;
    }
 }
 $c->set_stat( name_en=>"inserted_from_$site_address", name_ru=>"Последнее добавление с $site_address", val=>$total_inserted, 
    comment=>"(кол-во всех новых конкурсов)"  )  if $total_inserted; 

 return 1; # this say: "Ok or not"
}
 
sub insert_from_page{
 # (most return number of real inserted_news)
 my $self = shift;
 my %pars = @_;
 my $c = $pars{c} or die "c!";
 my $url = $pars{url} or die "url!";
 my $m = $pars{m} or die "m!";
 my $news = $pars{news} or die "news!";
 my $page_n = $pars{page_n} or die "page_n!";
 
 
 $m->get( $url );
 
 # раздел "планируемые" расположен на нескольких страницах, 
 # страницы перелистываются через форму
 $m->form_name('aspnetForm');
 $m->field('__EVENTTARGET','ctl00$phWorkZone$pgrNew'); # 'ctl00$phWorkZone$pgrNew' - это строка, (не переменная)
 $m->field('__EVENTARGUMENT',$page_n);
 $m->submit();
 
 $c->debug_explain("get content\n");
 my $co = $m->content;

 my $data_dir = $c->{data_dir} or die "dir!";
 
# my $f = $c->{list_file} or die "file!";
# my $url_to_file = $url;
# $url_to_file =~ s/(http\:\/\/)|[\/\:\.]/\_/g;
 my $current_file = "$data_dir/list_of_$page_n.list";
 open FILE, ">$current_file" or die "Can't open $current_file: $!";
 $co = encode("utf8",$co);
 print FILE $co;
 close FILE;
# `firefox $current_file`;
 
 my $links_file = "$data_dir/links_$page_n.list";
 
 ( $co ) = $co =~ /(.+?)Текущие\s+закупки/smi; # начиная с текущих закупок все удалить
# die Dumper $co;
 
 open FILE , ">$links_file" or die "Can't open $links_file: $!";

 # LIKE THIS:
 # <td width="60"><font face="Tahoma,Verdana,Arial,sans-serif" color="Black">
 # <input type="image" alt="Подробнее" onclick='javascript: return VP(564362);' src="../images/Icons/document_info.gif" />
 # <input type="image" alt="Извещение" onclick='javascript: return VN(564362);' src="../images/Icons/document_print.gif" />
 # </font></td><td width="10%"><font face="Tahoma,Verdana,Arial,sans-serif" color="Black">09/2-003596</font></td>
 # <td><font face="Tahoma,Verdana,Arial,sans-serif" color="Black">Закупка шовного материала для отделения сосудистой хирургии на 4 квартал
 # 2009г по заявке  ГУЗ Брянская Областная больница №1.  </font></td>


 #my $l_regexp = '<td.+?return VP\((\d+)\).+?<\/td><td.+?<\/td><td>(.+?)<\/td>'; # already escaped
# $l_regexp =~ s|([\?])|\\$1|g;
# $c->debug_explain("regexp = '$l_regexp'\n");
 my %l = $co =~ m|VP\((\d+)\).*?/>.*?<td><font face\=.*?>(.+?)</font></td>|imsg;
 $c->debug_explain("Founded links:", Dumper \%l , "It will Saved into file '$links_file'.");
 print FILE "$_ | $l{$_} \n" for keys %l;
 close FILE;


 
 my $inserted_cnt = 0;
 for my $key ( keys %l ){

    $news->{ id } = $key;
    $news->{ url } = "/Tender/ViewPurchase.aspx?PurchaseId=$key";
    $news->{ small_text } = "$l{$key}";
    
    # нужен заказчик, а чтобы его увидеть нужно пройти про url:
    $c->debug_explain("Go to page id='$key'...\n");
    $m->get( $news->{ url } );
    $c->debug_explain("Click button...\n");
    $m->click_button( name=>'ctl00$phWorkZone$menu$__theTabStrip$ctl02$__theTab' );
    my $co = $m->content;
    $co or $self->{error} = "Can't get content. Site not work? (". __FILE__." line ".__LINE__.")\n", return 0;
    $co = encode("utf8",$co);
#    open TEST , ">test.html";
#    print TEST $co;
#    close TEST;
#    `firefox test.html`;
#    die;
#Заказчик</b></font></th><th scope="col"><font face="Tahoma,Verdana,Arial,sans-serif" color="Black"><b>Контактное лицо</b></font></th>
# </tr><tr align="left" bgcolor="White">
# <td><font face="Tahoma,Verdana,Arial,sans-serif" color="Black"> 
# <input type="image" name="ctl00$phWorkZone$Positions$ctl02$btEdit" 
# id="ctl00_phWorkZone_Positions_ctl02_btEdit" src="../images/Icons/document_info.gif" alt="Просмотр лота" border="0" />
#  </font></td>
#<td><font face="Tahoma,Verdana,Arial,sans-serif" color="Black">приобретение операционного светильника</font></td>
# <td><font face="Tahoma,Verdana,Arial,sans-serif" color="Black">ГУЗ&quot;Брянский областной онкологический диспансер&quot;</font></td><td>
    
    my ($cu) = $co =~/Заказчик.+?<td>.+?<td>.+?<td>\s*<font.+?>(.+?)<\/font/ism;
    $c->debug_explain("customer='$cu'\n");
#    die;
    $cu or $c->debug_explain("customer not found.\n");
    $news->{customer} = $cu;
    
#    # set_news() return 0-after error, 1-after insert news, 2-if news already exist and skipped, 3-if exist and updated
    my $set_rv = $c->set_news( $news, { update => 1 } ) 
	or $self->{error} = "Error while call set_news() (". __FILE__." line ".__LINE__."):\n ".$c->error(), return 0 ;
    $c->debug_explain(Dumper $news, "Set_news result: $set_rv");
    $inserted_cnt ++ if $set_rv == 1;
 }

 
# test filling database:
# for my $id (1..5){
#    $news->{ id } = $id;
#    $news->{ url } = "$site_address/test/$id";
#    $news->{ small_text } = "test text test text for your test $id $id $id test ";
#    
#    # set_news() return: 
     # 0-after error, 1-after insert news, 2-if news already exist and skipped, 3- if exist and updated (most set {update=>1} as 2-nd param)
#    $c->set_news( $news, {update=>1} ) or $self->{error} = "Error while call set_news() (". __FILE__." line ".__LINE__."):\n ".$c->error(), return 0 ;
# } 
 
 return $inserted_cnt;
}

sub new{
 my $package = shift;
 my $self = {
    site_address => $site_address,
    
 };
 return bless $self;
}

sub error{ shift->{error} }

sub enabled{ return $enabled }

1;
